package Offer32;

import java.util.*;

/**
 * 请实现一个函数按照之字形顺序打印二叉树，即第一行按照从左到右的顺序打印，第二层按照从右到左的顺序打印.
 * 第三行再按照从左到右的顺序打印，其他行以此类推。
 */
public class Test_three {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode2.left = treeNode4;
        treeNode2.right = treeNode5;
        List<List<Integer>> lists = new Solution().levelOrder(treeNode1);
        System.out.println(lists);
        Iterator<List<Integer>> iterator = lists.iterator();
        List<Integer> list = new ArrayList<>();
        Iterator<Integer> iterator1 = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null) {
            return lists;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            LinkedList<Integer> list = new LinkedList<>();
            for (int i = queue.size(); i > 0 ; i--) {
                TreeNode node = queue.poll();
                //这里特别要注意，是用Lists的大小和2取余判断奇偶层，不是使用queue，因为queue的层数不能代表整个层都遍历完
                if (lists.size()%2 == 0) {
                    //Java中的java.util.LinkedList.addLast()方法用于在LinkedList的末尾插入特定元素。
                    list.addLast(node.val);
                }else {
                    //Java中的java.util.LinkedList.addFirst()方法用于在LinkedList的头部插入特定元素。
                    list.addFirst(node.val);
                }
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            lists.add(list);
        }
        return lists;
    }
}

/*这个想法思想是对的，但是实现有问题，是不成熟的解法，只能过一半的样例*/
class MyThinkingSolution{
    public List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList<List<Integer>> lists = new ArrayList<>();
        if (root == null) {
            return lists;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            for (int i = queue.size(); i > 0 ; i--) {
                if (lists.size()%2 == 0) {
                    TreeNode node = queue.poll();
                    list.add(node.val);
                    if (node.right != null) {
                        queue.add(node.right);
                    }
                    if (node.left != null) {
                        queue.add(node.left);
                    }
                }else {
                    TreeNode node = queue.poll();
                    list.add(node.val);
                    if (node.left != null) {
                        queue.add(node.left);
                    }
                    if (node.right != null) {
                        queue.add(node.right);
                    }
                }
            }
            lists.add(list);
        }
        return lists;
    }
}

/**
 * 二刷
 */
class SolutionThreeTwice{
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<List<Integer>> list = new LinkedList<>();
        while (!queue.isEmpty()) {
            LinkedList<Integer> list1 = new LinkedList<>();
            for(int i = queue.size(); i>0; i--){
                TreeNode poll = queue.poll();
                if ((list.size() & 1) == 0) {
                    list1.addLast(poll.val);
                }else {
                    list1.addFirst(poll.val);
                }
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if (poll.right != null) {
                    queue.add(poll.right);
                }
            }
            list.add(list1);
        }
        return list;
    }
}
